import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
import 'package:lai_zhan/common/redux/AppState.dart';
import 'package:lai_zhan/common/style/AppStyle.dart';
import 'package:lai_zhan/common/util/NavigatorUtil.dart';
import 'package:lai_zhan/widget/BaseScaffold.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:lai_zhan/common/model/userResult/user_result.dart';
import 'package:lai_zhan/common/model/shopResult/shop_result.dart';
import 'package:lai_zhan/common/model/activity/activity.dart';
import 'package:lai_zhan/widget/FlexableIconButton.dart';
import 'package:lai_zhan/common/config/Config.dart';
import 'package:lai_zhan/widget/ShopItem.dart';
import 'package:lai_zhan/common/dao/ActivityDao.dart';

class HomeTab extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _HomeTabState();
  }
}

class _HomeTabState extends State<HomeTab> {
  Store<AppState> store;
  bool hasInit = false;
  var images = [
    'lib/images/image1.jpg',
    'lib/images/image2.jpg',
    'lib/images/image3.jpg'
  ];
  UserResult coche; //私教
  List<ShopResult> shops;
  List<Activity> activitys = new List();
  ShopResult shop = new ShopResult('121', '海曙', '王大',
      'aaa', '17888888888', '宁波海曙区', '设施齐全', 1, 5);
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    store = StoreProvider.of(context);
    if (hasInit) return;
    hasInit = false;

    ///获取3活动
    /// 获取1私教
    /// 获取3店铺
    ActivityDao.get3Activity(store).then((dataResult){
      activitys = dataResult.data;
    });
  }

  //轮询内容
  List<Widget> _renderSliderItems() {
//    store.state.activitys.map((activit){
//      new Image.asset(name);
//    }).toList();
    return images.map((activity) {
      return new Padding(
        padding: EdgeInsets.all(3),
        child: new GestureDetector(
          child: ClipRRect(
            borderRadius: BorderRadius.circular(15),
            child: new Container(
              width: AppSize.silderImageWidth,
              height: AppSize.silderImageWidth * 9 / 16,
              child: new Image.asset(
                activity,
                fit: BoxFit.fill,
              ),
            ),
          ),
          onTap: () {
            //跳转活动界面
            NavigatorUtil.goActivityPage(context, Activity.Empty());
          },
        ),
      );
    }).toList();
  }

  //
  Widget _renderSlider() {
    return new CarouselSlider(
      items: _renderSliderItems(),
      viewportFraction: 0.9,
      initialPage: 0,
      aspectRatio: 16 / 7,
      reverse: false,
      autoPlay: true,
      interval: const Duration(seconds: 5),
      autoPlayCurve: Curves.fastOutSlowIn,
      autoPlayDuration: const Duration(milliseconds: 1000),
      distortion: true,
    );
  }

  //按钮
  Widget _renderButtons() {
    double height = AppSize.getRealHeight(55);
    return new Container(
      margin: EdgeInsets.only(left: 10, right: 10),
      child: new Column(
        children: <Widget>[
          new Row(
            children: <Widget>[
              new FlexableIconButton(
                height: height,
                flex: 2,
                backgroundColor: Colors.amber,
                text: '团体课',
                fontColor: Colors.white,
                fontSize: AppConstant.bigTextSize,
                iconData: AppIcon.TEAM,
                iconColor: Colors.white,
                iconSize: AppSize.homeIconSize,
                onClick: (){
                  NavigatorUtil.goCourseList(context);
                },
              ),
              new Padding(padding: EdgeInsets.all(2)),
              new FlexableIconButton(
                height: height,
                flex: 3,
                backgroundColor: Colors.blue,
                text: '私教课',
                fontColor: Colors.white,
                fontSize: AppConstant.bigTextSize,
                iconData: AppIcon.CYCLE,
                iconColor: Colors.white,
                iconSize: AppSize.homeIconSize,
                onClick: (){
                  NavigatorUtil.goCoachList(context);
                },
              ),
            ],
          ),
          new Padding(padding: EdgeInsets.all(2)),
          new Row(
            children: <Widget>[
              new FlexableIconButton(
                height: height,
                flex: 3,
                backgroundColor: Colors.red,
                text: '莱战活动',
                fontColor: Colors.white,
                fontSize: AppConstant.bigTextSize,
                iconData: AppIcon.FITNESS_ROOM2,
                iconColor: Colors.white,
                iconSize: AppSize.homeIconSize,
              ),
              new Padding(padding: EdgeInsets.all(2)),
              new FlexableIconButton(
                height: height,
                flex: 2,
                backgroundColor: Colors.black,
                text: '充值',
                fontColor: Colors.white,
                fontSize: AppConstant.bigTextSize,
                iconData: AppIcon.CARD,
                iconColor: Colors.white,
                iconSize: AppSize.homeIconSize,
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _renderShop() {
    return new Container(
        child: new Column(
          children: <Widget>[
            new Container(margin: EdgeInsets.only(left: 10,right: 10),
              child: new Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.end,
                children: <Widget>[
                  new Text(
                    '推荐门店',
                    style: AppConstant.largeTextBold,
                  ),
                  new GestureDetector(child: new Text('所有门店>>', style: AppConstant.middleSubLightText),
                    onTap: (){
                      NavigatorUtil.goShopList(context);
                    },
                  )
                ],
              ),
            ),
            new Container(margin: EdgeInsets.only(top: 10,),height: 1,color: Color(AppColors.white),),
            new ShopItem(shop: shop,),
            new Container(height: 5,color: Color(AppColors.white),),
            new ShopItem(shop: shop,),
            new Container(height: 5,color: Color(AppColors.white),),
            new ShopItem(shop: shop,),
          ],
        ));
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new BaseScaffold(
      title: '莱战健身',
      titleColor: Color(AppColors.white),
      body: new ListView(
        primary: false,
        children: <Widget>[
          new Column(
            children: <Widget>[
              new Padding(padding: EdgeInsets.all(5)),
              _renderSlider(),
              new Container(height: 10,color: Color(AppColors.white),
                margin: EdgeInsets.only(top: 10,bottom: 15),),
              _renderButtons(),
              new Container(height: 10,color: Color(AppColors.white),
                margin: EdgeInsets.only(top: 10,bottom: 5),),
              _renderShop(),
            ],
          )
        ],
      ),
    );
  }
}
